package com.fzn3.day03;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;

import java.util.Arrays;
import java.util.Collections;

/**
 * @author fzn
 * 描述：  双向环形链表-带头结点测试
 * @program DataStructure
 * @create 2023/3/9 13:09
 **/
public class TestCircularLinkedList {
    @Test
    @DisplayName("addFirst")
    public void test1() {
        CircularLinkedList circularLinkedList = getListFist();
        Assertions.assertIterableEquals(Arrays.asList(5, 4, 3, 2, 1), circularLinkedList);
    }

    @Test
    @DisplayName("addLast")
    public void test2() {
        CircularLinkedList circularLinkedList = getList();
        Assertions.assertIterableEquals(Arrays.asList(1, 2, 3, 4, 5), circularLinkedList);
    }

    @Test
    @DisplayName("removeFirst")
    public void test3() {
        CircularLinkedList circularLinkedList = getList();
        circularLinkedList.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(2, 3, 4, 5), circularLinkedList);
        circularLinkedList.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(3, 4, 5), circularLinkedList);
        circularLinkedList.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(4, 5), circularLinkedList);
        circularLinkedList.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(5), circularLinkedList);
        circularLinkedList.removeFirst();
        Assertions.assertIterableEquals(Collections.emptyList(), circularLinkedList);
        Assertions.assertThrows(IllegalArgumentException.class, circularLinkedList::removeFirst);
    }

    @Test
    @DisplayName("removedLast")
    public void test4() {
        CircularLinkedList circularLinkedList = getList();
        circularLinkedList.removedLast();
        Assertions.assertIterableEquals(Arrays.asList(1, 2, 3, 4), circularLinkedList);
        circularLinkedList.removedLast();
        Assertions.assertIterableEquals(Arrays.asList(1, 2, 3), circularLinkedList);
        circularLinkedList.removedLast();
        Assertions.assertIterableEquals(Arrays.asList(1, 2), circularLinkedList);
        circularLinkedList.removedLast();
        Assertions.assertIterableEquals(Arrays.asList(1), circularLinkedList);
        circularLinkedList.removedLast();
        Assertions.assertIterableEquals(Collections.emptyList(), circularLinkedList);
        Assertions.assertThrows(IllegalArgumentException.class, circularLinkedList::removedLast);
    }

    @Test
    @DisplayName("removeByValue")
    public void test5() {
        CircularLinkedList circularLinkedList = getList();
        circularLinkedList.removeByValue(1);
        Assertions.assertIterableEquals(Arrays.asList(2, 3, 4, 5), circularLinkedList);
        circularLinkedList.removeByValue(2);
        Assertions.assertIterableEquals(Arrays.asList(3, 4, 5), circularLinkedList);
        circularLinkedList.removeByValue(3);
        Assertions.assertIterableEquals(Arrays.asList(4, 5), circularLinkedList);
        circularLinkedList.removeByValue(4);
        Assertions.assertIterableEquals(Collections.singletonList(5), circularLinkedList);
        circularLinkedList.removeByValue(5);
        Assertions.assertIterableEquals(Collections.emptyList(), circularLinkedList);
        circularLinkedList.removeByValue(6);
        Assertions.assertIterableEquals(Collections.emptyList(), circularLinkedList);
    }

    private static CircularLinkedList getList() {
        CircularLinkedList circularLinkedList = new CircularLinkedList();
        circularLinkedList.addLast(1);
        circularLinkedList.addLast(2);
        circularLinkedList.addLast(3);
        circularLinkedList.addLast(4);
        circularLinkedList.addLast(5);
        return circularLinkedList;
    }

    private static CircularLinkedList getListFist() {
        CircularLinkedList circularLinkedList = new CircularLinkedList();
        circularLinkedList.addFirst(1);
        circularLinkedList.addFirst(2);
        circularLinkedList.addFirst(3);
        circularLinkedList.addFirst(4);
        circularLinkedList.addFirst(5);
        return circularLinkedList;
    }


}
